package com.ymatou.iapi.m.couponservice.testcase;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.m.couponservice.parameter.BatchInfo;
import com.ymatou.iapi.m.couponservice.parameter.CreateBean;
import com.ymatou.iapi.m.couponservice.parameter.CreateCouponBean;
import com.ymatou.iapi.m.couponservice.parameter.GetBatchInfoBean;
import com.ymatou.iapi.m.couponservice.parameter.SellerCreateBatchBean;
import com.ymatou.iapi.m.couponservice.service.CreateCall;
import com.ymatou.iapi.m.couponservice.service.CreateCouponCall;
import com.ymatou.iapi.m.couponservice.service.GetBatchInfoCall;
import com.ymatou.iapi.m.couponservice.service.SellerCreateBatchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponServiceMCallService;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
/**
*获取批次信息用例
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetBatchInfo{
	private static GetBatchInfoBean getbatchinfoBean;
	private static GetBatchInfoCall getbatchinfoCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取批次信息用例");
	}

	@Before
	public void caseUp() {
		getbatchinfoBean=new GetBatchInfoBean();
		getbatchinfoCall=new GetBatchInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getbatchinfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_GetBatchInfo_001() {
		Logger.start(true,"获取批次信息-短码");
		try {
			List<Map> list = new YmtCouponBatchWapper().getTop1PlatformShortCodeCouponInfo();

			getbatchinfoBean.setBatchCode(list.get(0).get("sBatchCode").toString());
			getbatchinfoCall.setData(getbatchinfoBean);
			getbatchinfoCall.callService();			
			
			VerifyReturn(list.get(0));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBatchInfo_002() {
		Logger.start(true,"获取批次信息-链接");
		try {
			List<Map> list = new YmtCouponBatchWapper().getTop1PlatformLinkCouponInfo();

			getbatchinfoBean.setBatchCode(list.get(0).get("sBatchCode").toString());
			getbatchinfoCall.setData(getbatchinfoBean);
			getbatchinfoCall.callService();			
			
			VerifyReturn(list.get(0));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBatchInfo_003() {
		Logger.start(true,"获取批次信息-商家优惠券");
		try {
			List<Map> list = new YmtCouponBatchWapper().getTop1SellerCouponInfo();

			getbatchinfoBean.setBatchCode(list.get(0).get("sBatchCode").toString());
			getbatchinfoCall.setData(getbatchinfoBean);
			getbatchinfoCall.callService();			
			
			VerifyReturn(list.get(0));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBatchInfo_004() {
		Logger.start(true,"获取批次信息-用于兑换的批次");
		try {
			
			//生成批次及兑换码
			CouponServiceMCallService couponCallService = new CouponServiceMCallService();
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();

			CreateCouponCall createCouponCall = couponCallService.createPlatformCoupon(createCouponBean);

			CouponServiceMCallService mCouponserviceCallService = new CouponServiceMCallService();

			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());

			CreateBean createBean = mCouponserviceCallService.setDefaultRandomCreateBean(batchCodes);
			CreateCall createCall = mCouponserviceCallService.create(createBean);
			
			List<Map> list = new YmtCouponBatchWapper().selectBatchInfoBysBatchCode(createCouponCall.getBatchCode());

			getbatchinfoBean.setBatchCode(list.get(0).get("sBatchCode").toString());
			getbatchinfoCall.setData(getbatchinfoBean);
			getbatchinfoCall.callService();			
			
			VerifyReturn(list.get(0));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBatchInfo_006() {
		Logger.start(true,"获取批次信息-商家优惠券-分享券");
		try {
			// 分享券	
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellercreatebatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			
			sellercreatebatchBean.setCouponType((byte)2);
			sellercreatebatchBean.setEffectiveDays(2);
		
			SellerCreateBatchCall sellerCreateBatchCall = couponServiceMCallService.SellerCreateBatch(sellercreatebatchBean);
		
			getbatchinfoBean.setBatchCode(sellerCreateBatchCall.getBatchCode());
			getbatchinfoCall.setData(getbatchinfoBean);
			getbatchinfoCall.callService();			
			
			 Logger.verifyEquals((byte)2, getbatchinfoCall.getBatchInfo().getCouponType(),
		                "验证返回coupontype");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void VerifyReturn(Map item) {
        Logger.verifyEquals("0", getbatchinfoCall.getCode(),
                "验证返回code");
     
        Logger.verifyEquals("操作成功！",
                getbatchinfoCall.getMessage(), "验证返回Message");
        
        BatchInfo batchInfo = getbatchinfoCall.getBatchInfo();

        String batchCode = getbatchinfoBean.getBatchCode();

        Logger.verifyEquals(Integer.parseInt(item.get("iBatchId").toString()),
        		batchInfo.getBatchId(),
                "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                        + "记录中iBatchId字段相同");
        
        Logger.verifyEquals(Integer.parseInt(item.get("iScenarioType").toString()), batchInfo.getProductLimitType(),
				"验证返回字段productLimitType与Ymt_CouponSetting表中iScenarioType一致");
        
        Logger.verifyEquals(item.get("sBatchCode").toString(),
        		batchInfo.getBatchCode(),
                "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                        + "记录中sBatchCode字段相同");
        
        // 验证AcquireStartTime，AcquireEndTime，AcquireCouponMethod
        if(item.get("AcquireStartTime")!=null&&batchInfo.getAcquireStartTime()!=null)
        {
        	String acquireStartTime =item.get("AcquireStartTime").toString() ;
        	Logger.verifyEquals(acquireStartTime.substring(0, 19).replace(" ", "T"),
        		batchInfo.getAcquireStartTime(),
                "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                        + "记录中AcquireStartTime字段相同");
        }
        if(item.get("AcquireEndTime")!=null&&batchInfo.getAcquireEndTime()!=null)
        {
        	Logger.verifyEquals(item.get("AcquireEndTime").toString().substring(0, 19).replace(" ", "T"),
        		batchInfo.getAcquireEndTime(),
                "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                        + "记录中AcquireEndTime字段相同");
        }
        
         
        // 是否作废
        if(item.get("bInvalidStatus")==null)
        {
        	Logger.verifyEquals(false,
            		batchInfo.getIsExpire(),
                    "验证返回IsCancellation与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                            + "记录中bInvalidStatus字段相同");
        	
        	Logger.verifyEquals(false,
            		batchInfo.getExpire(),
                    "验证返回IsCancellation与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                            + "记录中bInvalidStatus字段相同");
        }
        else {
        	Logger.verifyEquals(Boolean.parseBoolean(item.get("bInvalidStatus").toString()),
            		batchInfo.getIsExpire(),
                    "验证返回IsCancellation与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                            + "记录中bInvalidStatus字段相同");
		}
        
     // 是否领完
        Logger.verifyEquals(Integer.parseInt(item.get("iCouponTotalNum").toString())==Integer.parseInt(item.get("iReceiveCount").toString()),
        		batchInfo.getReceiveOver(),
                "验证返回IsReceiveOver正确");
           
   
        Logger.verifyEquals(Integer.parseInt(item.get("iSendType").toString()),
        		batchInfo.getSendType(),
                "验证返回SendType与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                        + "记录中iSendType字段相同");
        
        if(item.get("iEffectiveType")==null)
        {
        	Logger.verifyEquals(0,
        		batchInfo.getEffectiveType(),
                "验证返回EffectiveType与数据库中表Ymt_CouponSetting中iEffectiveType字段相同");
        }
        else{
        	   Logger.verifyEquals(Integer.parseInt(item.get("iEffectiveType").toString()),
               		batchInfo.getEffectiveType(),
                       "验证返回EffectiveType与数据库中表Ymt_CouponSetting中iEffectiveType字段相同");
        }
        
        if(item.get("iEffectiveType")==null)
        {
        	 Logger.verifyEquals(0,
             		batchInfo.getEffectiveValidType(),
                     "验证返回EffectiveValidType与数据库中表Ymt_CouponSetting中iEffectiveValidType字段相同");
             }
        else
        {
        	 Logger.verifyEquals(Integer.parseInt(item.get("iEffectiveValidType").toString()),
             		batchInfo.getEffectiveValidType(),
                     "验证返回EffectiveValidType与数据库中表Ymt_CouponSetting中iEffectiveValidType字段相同");
        
        }

        Logger.verifyEquals(item.get("dValidStart").toString().subSequence(0, 19),
        		batchInfo.getValidStart().replace("T", " "),
                "验证返回dValidStart与数据库中表Ymt_CouponSetting中dValidStart字段相同");

        Logger.verifyEquals(item.get("dValidEnd").toString().subSequence(0, 19),
        		batchInfo.getValidEnd().replace("T", " "),
                "验证返回dValidEnd与数据库中表Ymt_CouponSetting中dValidEnd字段相同");
        
        if(item.get("iEffectiveDays")==null)
        {
        	Logger.verifyEquals(0,
        		batchInfo.getEffectiveDays(),
                "验证返回iEffectiveDays与数据库中表Ymt_CouponSetting中iEffectiveDays字段相同");
        }
        else {
        	Logger.verifyEquals(Integer.parseInt(item.get("iEffectiveDays").toString()),
            		batchInfo.getEffectiveDays(),
                    "验证返回iEffectiveDays与数据库中表Ymt_CouponSetting中iEffectiveDays字段相同");     
		}
        
        Logger.verifyEquals(Integer.parseInt(item.get("iCouponTotalNum").toString()),
        		batchInfo.getTotalCount(),
                "验证返回TotalCount与数据库中表Ymt_CouponBatch中iCouponTotalNum字段相同");
        
        Logger.verifyEquals(Integer.parseInt(item.get("iReceiveCount").toString()),
        		batchInfo.getReceivedCount(),
                "验证返回BatchCode与数据库中表Ymt_CouponSetting中iReceiveCount字段相同");
        
        if(item.get("IsShowInPage")==null)
        {
        	Logger.verifyEquals(false,
        		batchInfo.getIsShowInPage(),
                "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                        + "记录中IsShowInPage字段相同");
        }
        else {
            Logger.verifyEquals(Boolean.parseBoolean(item.get("IsShowInPage").toString()),
            		batchInfo.getIsShowInPage(),
                    "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                            + "记录中IsShowInPage字段相同");
		}
        
        Logger.verifyEquals(Integer.parseInt(item.get("iUserType").toString()),
        		batchInfo.getUserType(),
                "验证返回BatchCode与数据库中表Ymt_CouponScenario中iUserType字段相同");
        
        if(item.get("sSellerIds")==null)
        {
        	Logger.verifyEquals(null,
        		batchInfo.getSellerIds(),
                "验证返回BatchCode与数据库中表Ymt_CouponScenario中sSellerIds字段相同");
        }
        else {
        	Logger.verifyEquals(item.get("sSellerIds").toString(),
            		batchInfo.getSellerIds(),
                    "验证返回BatchCode与数据库中表Ymt_CouponScenario中sSellerIds字段相同");
		}
        
        if(item.get("sProductCategories")==null)
        {
        	Logger.verifyEquals(null,
        		batchInfo.getProductCategories(),
                "验证返回BatchCode与数据库中表Ymt_CouponScenario中sProductCategories字段相同");
        }
        else {
        	Logger.verifyEquals(item.get("sProductCategories").toString(),
            		batchInfo.getProductCategories(),
                    "验证返回BatchCode与数据库中表Ymt_CouponScenario中sProductCategories字段相同");
		}
        
        if(item.get("sSpecificProducts")==null)
        {
        	Logger.verifyEquals(null,
        		batchInfo.getSpecificProducts(),
                "验证返回BatchCode与数据库中表Ymt_CouponScenario中sSpecificProducts字段相同");
        }
        else {
        	Logger.verifyEquals(item.get("sSpecificProducts").toString(),
            		batchInfo.getSpecificProducts(),
                    "验证返回BatchCode与数据库中表Ymt_CouponScenario中sSpecificProducts字段相同");
		}
        
        if(item.get("sProductBrands")==null)
        {
        	Logger.verifyEquals(null,
        		batchInfo.getProductBrands(),
                "验证返回BatchCode与数据库中表Ymt_CouponScenario中sProductBrands字段相同");
        }
        else {
        	 Logger.verifyEquals(item.get("sProductBrands").toString(),
             		batchInfo.getProductBrands(),
                     "验证返回BatchCode与数据库中表Ymt_CouponScenario中sProductBrands字段相同");
          
		}
        
        if(item.get("sActivityIds")==null)
        {
        	Logger.verifyEquals(null,
        		batchInfo.getActivityIds(),
                "验证返回BatchCode与数据库中表Ymt_CouponScenario中sActivityIds字段相同");
        }
        else {
        	  Logger.verifyEquals(item.get("sActivityIds").toString(),
              		batchInfo.getActivityIds(),
                      "验证返回BatchCode与数据库中表Ymt_CouponScenario中sActivityIds字段相同");        
		}
        
        if(item.get("sUsePlatforms")==null)
        {
        	Logger.verifyEquals(null,
        		batchInfo.getUsePlatforms(),
                "验证返回BatchCode与数据库中表Ymt_CouponScenario中sUsePlatforms字段相同");
        }
        else {
        	Logger.verifyEquals(item.get("sUsePlatforms").toString(),
            		batchInfo.getUsePlatforms(),
                    "验证返回BatchCode与数据库中表Ymt_CouponScenario中sUsePlatforms字段相同");
		}
        
        // 验证coupontype-分享券
        Logger.verifyEquals(Byte.parseByte(item.get("CouponType").toString()),
        		batchInfo.getCouponType(),
                 "验证返回BatchCode与数据库中表Ymt_CouponBatch中coupontype字段相同");

    }
}